﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace nMongo
{
    public partial class MongoEngine
    {
        /// <summary>
        /// Retrieves all the objects inside the current collection.
        /// </summary>
        /// <returns></returns>
        public MongoQueryCollection<T> Select<T>()
            where T : class
        {
            MongoQueryCollection<T> mongoEnum = new MongoQueryCollection<T>(this, this.Database.GetCollectionFromType<T>().Queryable().ToList());

            //if (returned.Count() > 0)
            //{
            //    foreach (T item in returned)
            //    {
            //        this.AutoDetectPersistencyByObject(MongoPersistencyState.Retrieved, item);
            //    }
            //}
            return mongoEnum;
        }

        /// <summary>
        /// Retrieves all the objects inside the current collection, giving a specified condition.
        /// </summary>
        /// <param name="predicate">Expression to evaluate and determines the condition satisfaction.</param>
        public MongoQueryCollection<T> Select<T>(Expression<Func<T, bool>> predicate)
            where T : class
        {
            MongoQueryCollection<T> mongoEnum = new MongoQueryCollection<T>(this, this.Database.GetCollectionFromType<T>().Queryable().Where(predicate).ToList());

            //if (returned.Count() > 0)
            //{
            //    foreach (T item in returned)
            //    {
            //        this.AutoDetectPersistencyByObject(MongoPersistencyState.Retrieved, item);
            //    }
            //}
            return mongoEnum;
        }
    }
}
